//
//  YSReviewsBottomView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/9.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSReviewsBottomView.h"
#import "YSCommentView.h"

#define kGrayColor HEXColor(@"#999493")
#define kPraiseFont 14
#define kPraiseInset 7
#define kTopMargin 8

@interface YSReviewsBottomView()<UITextFieldDelegate>
{
    UIView *_lineView;
    YSCommentView *_commentView;
    UIButton *_praiseBtn;
}

@end

@implementation YSReviewsBottomView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UITextField *inputField = [[UITextField alloc] init];
        inputField.borderStyle = UITextBorderStyleRoundedRect;
        inputField.keyboardType = UIKeyboardAppearanceDefault;
        inputField.returnKeyType = UIReturnKeySend;
        inputField.delegate = self;
        [inputField setFont:SYS_FONT(14)];
        [self addSubview:inputField];
        _inputField = inputField;
        
        
        YSCommentView *commentView = [[YSCommentView alloc] init];
        UIImage *commentImage  = [UIImage imageNamed:@"icon_message_gray"];
        [commentView setIconImage:commentImage];
        [commentView setNumBackgroundColor:kGrayColor];
        [self addSubview:commentView];
        _commentView = commentView;
        
        [commentView addTarget:self action:@selector(clickCommentView:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *praiseBtn = [[UIButton alloc] init];
        [praiseBtn setImage:[UIImage imageNamed:@"icon_praise"] forState:UIControlStateNormal];
        [praiseBtn setImage:[UIImage imageNamed:@"icon_praise_red"] forState:UIControlStateSelected];
        [praiseBtn setTitleColor:kGrayColor forState:UIControlStateNormal];
        praiseBtn.titleLabel.font = SYS_FONT(kPraiseFont);
        [self addSubview:praiseBtn];
        _praiseBtn = praiseBtn;
        
        [praiseBtn addTarget:self action:@selector(clickPraise:) forControlEvents:UIControlEventTouchUpInside];
        
    
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = HEXColor(@"#B9B9B9");
        [self addSubview:_lineView];
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    if(CGRectEqualToRect(frame, CGRectZero)) return;
    
    [self setChildFrame];
}

- (void)setChildFrame{
    // praise
    NSString *praiseNumStr = [NSString stringWithFormat:@"%lu",(unsigned long)_likeNum];
    CGFloat praiseWidth = _praiseBtn.imageView.image.size.width;
    if (_likeNum) {
        [_praiseBtn setImageEdgeInsets:UIEdgeInsetsMake(0.0, -kPraiseInset, 0.0, 0.0)];
        CGSize praiseFontSize = [praiseNumStr sizeWithFont:SYS_FONT(kPraiseFont)];
        praiseWidth += kPraiseInset + praiseFontSize.width;
    }
    
    [_praiseBtn setTitle:praiseNumStr forState:UIControlStateNormal];
    _praiseBtn.frame = CGRectMake(self.frame.size.width - praiseWidth - YSPublicXInset, kTopMargin, praiseWidth, self.frame.size.height - kTopMargin*2);
    
    // comment
    CGRect commentFrame =  _commentView.frame;
    commentFrame.origin.x = _praiseBtn.frame.origin.x - commentFrame.size.width - 20;
    commentFrame.origin.y = kTopMargin;
    commentFrame.size.height = self.frame.size.height - kTopMargin *2;
    _commentView.frame = commentFrame;
    
    //textField
    CGRect fieldFrame = CGRectMake(YSPublicXInset, kTopMargin, commentFrame.origin.x - 15 - YSPublicXInset, self.frame.size.height - kTopMargin*2);
    _inputField.frame = fieldFrame;
    
    //line
    _lineView.frame = CGRectMake(0, 0, self.size.width, 0.5);
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(ISBLANK(textField.text)){
        ShowTips(@"请输入内容");
        return YES;
    }
    if ([self.delegate respondsToSelector:@selector(publish:)]) {
        NSString *text = textField.text;
        if (_commentInfo) {
            text = [text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:kContentPrefix,_commentInfo.commentName] withString:@""];
        }
        [_delegate publish:text];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (!string.length) {
        if ([textField.text isEqualToString:[NSString stringWithFormat:kContentPrefix,_commentInfo.commentName]]) {
            textField.text = nil;
        }
    }
    return YES;
}


- (void)setLikeNum:(NSInteger)likeNum{
    _likeNum = likeNum;
    [self setChildFrame];
}

- (void)setIsLike:(NSInteger)isLike{
    _isLike = isLike;
    _praiseBtn.selected = isLike;
}

- (void)setReplyNum:(NSInteger)replyNum{
    _replyNum = replyNum;
    _commentView.num = replyNum;
}

// 点击去评论
- (void)clickCommentView:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(toComment)]) {
        [_delegate toComment];
    }
}

// 点赞
- (void)clickPraise:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(praise)]) {
        [_delegate praise];
    }
}

@end
